package com.cloud.order.netty.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;

/**
 * @Description:
 * @Author: ZhouShuai
 * @Date: 2021-07-07 11:31
 */
@Slf4j
@Component
public class NettyServer {

    public void start(InetSocketAddress address) {
        // 配置服务端的NIO线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap bootstrap =
                    new ServerBootstrap()
                            .group(bossGroup, workerGroup) // 绑定线程池
                            .channel(NioServerSocketChannel.class)
                            .handler(new LoggingHandler(LogLevel.INFO)) //打印日志
                            .localAddress(address)
                            .childHandler(new NettyServerChannelInitializer()) // 编码解码
                            .option(ChannelOption.SO_BACKLOG, 128) // 服务端接受连接的队列长度，如果队列已满，客户端连接将被拒绝
                            .childOption(ChannelOption.SO_KEEPALIVE, true); // 保持长连接，2小时无数据激活心跳机制

            // 绑定端口，开始接收进来的连接
            ChannelFuture future = bootstrap.bind(address).sync();
            log.info("netty服务器开始监听端口：" + address.getPort());
            // 关闭channel和块，直到它被关闭
            future.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
